#!/bin/bash

# 交互式自动化部署脚本
SERVER_IP="106.75.5.247"
USERNAME="ubuntu"
PASSWORD="we13599793044"

echo "🚀 开始交互式自动化部署到: $SERVER_IP"
echo "💡 密码是: $PASSWORD"
echo ""

# 1. 上传WAR文件
echo "📤 第1步：上传应用文件..."
echo "即将执行: scp target/OrderStatsSystem-1.0-SNAPSHOT.war $USERNAME@$SERVER_IP:/tmp/app.war"
echo "请在提示时输入密码: $PASSWORD"
read -p "按Enter键继续..."

scp target/OrderStatsSystem-1.0-SNAPSHOT.war $USERNAME@$SERVER_IP:/tmp/app.war

if [ $? -ne 0 ]; then
    echo "❌ 文件上传失败，请检查网络连接"
    exit 1
fi

echo "✅ 文件上传成功！"
echo ""

# 2. 执行部署命令
echo "🔧 第2步：执行远程部署..."
echo "即将连接服务器并执行部署命令"
echo "请在提示时输入密码: $PASSWORD"
read -p "按Enter键继续..."

# 创建远程执行脚本
cat > /tmp/deploy_commands.sh << 'EOF'
#!/bin/bash

echo "📦 更新系统并安装软件..."
sudo apt update -y
sudo DEBIAN_FRONTEND=noninteractive apt install -y openjdk-11-jdk tomcat9 tomcat9-admin nginx wget curl unzip sqlite3

echo "☕ 配置Java环境..."
sudo update-alternatives --set java /usr/lib/jvm/java-11-openjdk-amd64/bin/java

echo "🛑 停止现有服务..."
sudo systemctl stop tomcat9 2>/dev/null || true
sudo systemctl stop nginx 2>/dev/null || true

echo "🚀 部署应用..."
sudo rm -rf /var/lib/tomcat9/webapps/ROOT*
sudo cp /tmp/app.war /var/lib/tomcat9/webapps/ROOT.war
sudo chown tomcat:tomcat /var/lib/tomcat9/webapps/ROOT.war

echo "⚙️ 配置Tomcat..."
echo 'export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64' | sudo tee /etc/default/tomcat9
echo 'export CATALINA_OPTS="-Xmx2048m -Xms1024m -XX:+UseG1GC"' | sudo tee -a /etc/default/tomcat9

echo "🌐 配置Nginx..."
sudo tee /etc/nginx/sites-available/default > /dev/null << 'NGINXEOF'
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
NGINXEOF

echo "🔥 配置防火墙..."
sudo ufw allow OpenSSH 2>/dev/null || true
sudo ufw allow 80/tcp 2>/dev/null || true
sudo ufw allow 8080/tcp 2>/dev/null || true
sudo ufw --force enable 2>/dev/null || true

echo "🔧 启动服务..."
sudo systemctl start tomcat9
sudo systemctl enable tomcat9
sudo systemctl start nginx
sudo systemctl enable nginx

echo "⏳ 等待服务启动..."
sleep 30

echo "🔍 检查服务状态..."
sudo systemctl is-active tomcat9
sudo systemctl is-active nginx

echo "🎯 测试访问..."
curl -I http://localhost:8080 2>/dev/null || echo "Tomcat未就绪"
curl -I http://localhost 2>/dev/null || echo "Nginx未就绪"

echo "✅ 部署完成！"
echo "📱 访问地址: http://106.75.5.247"
echo "🔐 默认账号: admin / admin123"

rm -f /tmp/deploy_script.sh
EOF

# 上传部署脚本到服务器
echo "📤 上传部署脚本..."
scp /tmp/deploy_commands.sh $USERNAME@$SERVER_IP:/tmp/deploy_script.sh

# 执行部署脚本
echo "🚀 执行部署脚本..."
ssh -t $USERNAME@$SERVER_IP "chmod +x /tmp/deploy_script.sh && /tmp/deploy_script.sh"

echo ""
echo "🎉 自动化部署完成！"
echo "📱 访问地址: http://$SERVER_IP"
echo "🔐 默认账号: admin / admin123"
echo ""
echo "📊 如需查看日志："
echo "   ssh $USERNAME@$SERVER_IP"
echo "   sudo journalctl -u tomcat9 -f"

# 清理临时文件
rm -f /tmp/deploy_commands.sh 